package com.alan.camunda.boot.delegate;

import com.alan.camunda.boot.util.CacheableExpressionManager;
import com.alan.camunda.boot.util.ExpressionManagerHolder;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.impl.el.Expression;
import org.camunda.bpm.engine.impl.el.ExpressionManager;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 使用 Engine 的 ExpressionManager 计算表达式
 */
@Slf4j
@Component("juelDelegate")
public class JuelDelegate implements JavaDelegate {


    @Override
    public void execute(DelegateExecution execution) throws Exception {
        execution.setVariable("xxx", null);

    }


    private void demo1(DelegateExecution execution) {
        CacheableExpressionManager expressionManager = ExpressionManagerHolder.get();

        Expression expression = expressionManager.getExpression("${num1 + num2}");
        Object value = expression.getValue(execution);
        log.info("${num1 + num2} = {}", value);
        execution.setVariable("num3", value);


        Expression expression1 = expressionManager.getExpression("${num3 * 2}");
        Object value2 = expression1.getValue(execution);
        log.info("${num3 * 2} = {}", value2);
        execution.setVariable("num4", value2);

        Object value1 = expressionManager.getExpression("${execution.processInstanceId}").getValue(execution);
        log.info("processInstanceId = {}", value1);
    }

    private void demo2(DelegateExecution execution) {
        JSONArray table = new JSONArray();
        JSONObject row1 = new JSONObject();
        row1.put("name", "张三");
        row1.put("age", 12);

        JSONObject row2 = new JSONObject();
        row2.put("name", "李四");
        row2.put("age", 22);

        JSONObject row3 = new JSONObject();
        row3.put("name", "王五");
        row3.put("age", 32);

        table.add(row1);
        table.add(row2);
        table.add(row3);

        execution.setVariable("DbRead_table", table);

        CacheableExpressionManager expressionManager = ExpressionManagerHolder.get();
        // 无法直接获取到所有这列的值
        String expString = "${DbRead_table.age  }";
        Expression expression1 = expressionManager.getExpression(expString);
        Object value2 = expression1.getValue(execution);
        log.info("exp {} value is {}", expString, value2);

    }

    private void demo3(DelegateExecution execution) {
        execution.setVariable("Loop.index", 1);
        execution.setVariable("Loop_index", 2);
        execution.setVariable("$jobName", "dddd");
        execution.setVariable("#jobName", "fff");

       // Map<String, Object> variables = execution.getVariables();


        CacheableExpressionManager expressionManager = ExpressionManagerHolder.get();
        // 无法直接获取到所有这列的值
        String expString = "${ users }";
        Expression expression1 = expressionManager.getExpression(expString);
        Object value2 = expression1.getValue(execution);
        log.info("exp {} value is {}", expString, value2);
    }

}
